#!/bin/bash

# 安装包所在路径，需要带上包名
readonly PACKAGE_FULL_PATH=/home/yytek/install/redis/redis.tar.gz
# 安装主目录
readonly INSTALL_HOME=/usr/local/redis
# 配置目录
readonly CONF_PATH=/home/yytek/install/redis/conf

# 检查是否为root用户
if [[ "$UID" -ne 0 ]]; then
    echo "ERROR: the script must run as root"
    exit 3
fi

# 记录常规日志
function log_info(){
    echo "[$(date -d today +"%Y-%m-%d %H%M%S %:::z")] $1"
}

# 记录异常日志
function log_error(){
    echo -e "[$(date +"%Y-%m-%d %H%M%S %Z%:z")] [ERROR] $* \n"
    exit 1
}

# 检查结果
function check_result(){
    local ret_code=$1
    shift
    local error_msg=$*
    if [[ ${ret_code} -ne 0 ]]; then
        log_error ${error_msg}
    fi
}

# 校验参数
function check_param(){
    if [[ ! -n ${PACKAGE_FULL_PATH} ]] || [[ ! -n ${INSTALL_HOME} ]]; then
        log_error "Param: PACKAGE_FULL_PATH INSTALL_HOME USER_PASSWD can not be null"
    fi
    if [[ ! -f ${PACKAGE_FULL_PATH} ]]; then
        log_error "Param: PACKAGE_FULL_PATH is not a directory"
    fi
}

# 检查软件进程是否存在
function check_soft_process(){
    local redis_process_count=`ps -ef | grep ${INSTALL_HOME} | grep -vwE "grep|vi|vim|tail|cat" | wc -l`
    if [[ ${redis_process_count} -gt 0 ]]; then
        log_error "Please stop and uninstall the soft first"
    fi
}


# 初始化配置文件cnf
function init_my_cnf(){
    #创建配置目录
    #mkdir ${INSTALL_HOME}/bin/
    #复制配置文件到安装配置目录
    cp ${CONF_PATH}/redis.conf ${INSTALL_HOME}/bin/
}


# 安装软件
function install_soft(){

    echo    "---安装GCC---"
    yum install -y gcc
    echo    "---安装Redis---"
    # 创建安装主目录
    mkdir -p ${INSTALL_HOME}
    # 解压安装包到安装主目录
    tar -zxvf ${PACKAGE_FULL_PATH} -C ${INSTALL_HOME} > /dev/null 2>&1
    echo ">>> tar finish"
    check_result $? "unzip soft package error"
    # 找到安装包名
    local package_name=`ls ${INSTALL_HOME} | grep redis`
    # 移动解压后包到安装目录
    mv ${INSTALL_HOME}/${package_name}/* ${INSTALL_HOME}
    # 删除解压后包到安装目录
    rm -rf ${INSTALL_HOME}/${package_name}
    # 打开安装目录
    cd ${INSTALL_HOME}

    # 编译
    make test
    make
    echo    "---编译完成---"
    # 安装
    cd ${INSTALL_HOME}/src
    make install PREFIX=${INSTALL_HOME}
    echo    "---安装完成---"
    # 在安装目录下创建一个 bin 目录
    cd ${INSTALL_HOME}
    mkdir ${INSTALL_HOME}/bin
    cd ${INSTALL_HOME}/src

    # 将 redis-server 和 redis-cli 移动到 bin 目录中
    cp redis-server ${INSTALL_HOME}/bin/
    cp redis-cli ${INSTALL_HOME}//bin/

    # 初始化my.cnf
    init_my_cnf
    # 启动软件
    #${INSTALL_HOME}/bin/redis-server ${directory}/redis/bin/redis.conf

    #初始化启动
    powered_up_soft


}
#初始化启动
function powered_up_soft(){
  cd ${INSTALL_HOME}/
  sed -i "s:^EXEC=/usr/local/bin/redis-server:EXEC=${INSTALL_HOME}/bin/redis-server:g" ./utils/redis_init_script
  sed -i "s:^CLIEXEC=/usr/local/bin/redis-cli:CLIEXEC=${INSTALL_HOME}/bin/redis-cli:g" ./utils/redis_init_script
  sed -i  "$(sed -n '/^CONF=/=' ./utils/redis_init_script)a  CONF=${INSTALL_HOME}/bin/redis.conf" ./utils/redis_init_script
  cp ./utils/redis_init_script /etc/init.d/redis
  service redis start
  chkconfig redis on
}

# 启动MySQL
function start() {
    systemctl start mysql
}

# 停止MySQL
function stop() {
    systemctl stop mysql
}

# 检查MySQL状态
function status() {
    systemctl status mysql
}

# 安装软件
function install() {
    log_info "++++++++++ step 1 ++++++++++"
    check_param
    log_info "check_param finish"

    log_info "++++++++++ step 2 ++++++++++"
    check_soft_process
    log_info "check_process finish"

    log_info "++++++++++ step 3 ++++++++++"
    install_soft
    log_info "install_soft finish"
}

# 卸载MySQL
function uninstall() {
    # 如果MySQL仍启动则停止
    local mysql_process_count=`ps -ef | grep ${INSTALL_HOME} | grep -vwE "grep|vi|vim|tail|cat" | wc -l`
    if [[ ${mysql_process_count} -gt 0 ]]; then
        stop
    fi

    # 删除创建的文件
    rm -rf ${INSTALL_HOME}
    rm -rf /etc/init.d/mysql.server
    rm -rf /etc/my.cnf
    rm -rf /tmp/mysql.sock*

    # 删除配置的环境变量
    sed -i '/### MySQL_PATH_ENV_S/,/### MySQL_PATH_ENV_E/d' /etc/profile

    # 删除用户和用户组
    id mysql &> /dev/null
    if [[ $? -eq 0 ]]; then
        userdel  mysql
    fi
    log_info "uninstall MySQL success"
}

# 命令帮助
function usage() {
    echo "Usage: $PROG_NAME {start|stop|restart|status|install|uninstall}"
    exit 2
}

# 主流程

PROG_NAME=$0
ACTION=$1

case "$ACTION" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    status)
        status
    ;;
    install)
        install
    ;;
    uninstall)
        uninstall
    ;;
    *)
        usage
    ;;
esac
